//
//  VistorLoginView.swift
//  weibo
//
//  Created by 胡海波 on 15/6/25.
//  Copyright © 2015年 胡海波. All rights reserved.
//

import UIKit
import FFAutoLayout
// 创建访客视图协议
protocol VistorLoginViewDelegate: NSObjectProtocol {

    func visitorRegisterButtonClicked()
    func visitorLoginButtonClicked()
}

class VistorLoginView: UIView {

    // 设置代理属性
    weak var delegate: VistorLoginViewDelegate?

    override init(frame: CGRect) {

        super.init(frame: frame)

        setupUI()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func setupViewInfo(iconName: String, message: String, isHome: Bool) {

        messageLable.text = message

        if isHome {
            homeIconView.hidden = false
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        } else{
            homeIconView.hidden = true
            iconView.image = UIImage(named: iconName)

            sendSubviewToBack(maskIconView)
        }


    }

    // 启动动画
    func startAnimation() {

        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20

        anim.repeatCount = MAXFLOAT

        iconView.layer.addAnimation(anim, forKey: nil)
    }

///  监听用户注册事件
    func register(){
        delegate?.visitorRegisterButtonClicked()
    }
///  监听用户登陆事件
    func login() {
        delegate?.visitorLoginButtonClicked()
    }

    private func setupUI() {
        // 1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(loginButton)

        // 2.对每个控件添加自动布局

        // 2.1 iconView
        iconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -40))

        // 2.2 HomeIconView
        homeIconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: iconView, size: nil)

        // 2.3 messageLable
        messageLable.ff_AlignVertical(ff_AlignType.BottomCenter, referView: homeIconView, size: CGSizeMake(240, 40), offset: CGPoint(x: 0, y: 20))

        // 2.4 registerButton
        registerButton.ff_AlignVertical(ff_AlignType.BottomLeft, referView: messageLable, size: CGSizeMake(100, 40), offset: CGPoint(x: 0, y: 10))

        // 2.5 loginButton
        loginButton.ff_AlignVertical(ff_AlignType.BottomRight, referView: messageLable, size: CGSizeMake(100, 40), offset: CGPoint(x: 0, y: 10))

        // 2.6 遮盖视图
        maskIconView.ff_Fill(self, insets: UIEdgeInsets(top: 0, left: 0, bottom:160, right: 0))
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)

    }

    // MARK: - 懒加载
    ///  图标
    lazy var iconView: UIImageView = {

        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()

    ///  遮罩图片
    lazy var maskIconView: UIImageView = {

        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()

    /// 房子
    lazy var homeIconView: UIImageView = {

        let image = UIImage(named: "visitordiscover_feed_image_house")

        return UIImageView(image: image)
    }()

    /// 文字标签
    lazy var messageLable: UILabel = {

        let lable = UILabel(frame: CGRectMake(0, 0, 240, 40))

        lable.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"

        lable.textColor = UIColor.lightGrayColor()
        lable.font = UIFont.systemFontOfSize(14)
        lable.preferredMaxLayoutWidth = 224

        lable.numberOfLines = 2
        lable.sizeToFit()

        return lable
    }()

    /// 注册按钮
    lazy var registerButton: UIButton = {

        let btn = UIButton(frame: CGRectMake(0, 50, 100, 50))

        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)

        // 监听按钮点击
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
    }()

    /// 登陆按钮
    lazy var loginButton: UIButton = {

        let btn = UIButton(frame: CGRectMake(100, 300, 100, 50))

        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)

        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        // 监听按钮点击
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
    }()



}
